﻿Imports NewHeaderSystems.Archive.Core
Imports NewHeaderSystems.Archive.Data

Public Class SecurityMaintenance
    Inherits PageTemplate

    Private Sub LoadGrid()
        Me.ParentForm.Cursor = Cursors.WaitCursor
        Dim bs As New BindingSource
        bs.DataSource = New SortableBindingList(Of SecurityView)(DataScript.GetSecurities(txtTicker.Text, txtCusip.Text, txtDescription.Text))
        dgGrid.DataSource = bs
        For Each row As DataGridViewRow In dgGrid.Rows
            row.Cells("colModify").Value = "Modify"
            row.Cells("colDelete").Value = "Delete"
        Next
        dgGrid.Sort(dgGrid.Columns("colTicker"), System.ComponentModel.ListSortDirection.Ascending)
        Me.ParentForm.Cursor = Cursors.Default
        dgGrid.Focus()
    End Sub

    Private Sub SecurityMaintenance_PageEnter(ByVal sender As System.Object, ByVal e As Microsoft.Samples.Windows.Forms.Navigation.PageEventArgs) Handles MyBase.PageEnter
        ResetForm()
        LoadGrid()
    End Sub

    Private Sub dgGrid_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgGrid.CellContentClick
        If e.RowIndex = -1 Then Exit Sub
        Dim securityView = TryCast(dgGrid.Rows(e.RowIndex).DataBoundItem, SecurityView)
        Select Case e.ColumnIndex
            Case dgGrid.Columns("colModify").Index
                ShowEditDialog(securityView)
            Case dgGrid.Columns("colDelete").Index
                DeleteArchive(securityView.ID)
        End Select
    End Sub

    Private Sub DeleteArchive(ByVal ID As Integer)
        If MessageBox.Show("Delete the selected Security?", "Delete Security", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
            DataScript.DeleteSecurity(ID)
            LoadGrid()
        End If
    End Sub

    Private Sub btnFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        LoadGrid()
        btnClearFilter.Enabled = True

    End Sub

    Private Sub ResetForm()
        txtTicker.Text = ""
        txtCusip.Text = ""
        txtDescription.Text = ""
    End Sub

    Private Sub btnClearFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearFilter.Click
        ResetForm()
        LoadGrid()
        btnClearFilter.Enabled = False
    End Sub

    Private Sub SecurityMaintenance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Title = "Securities Maintenance"
        Description = "Maintain securities information."
        PageIcon = My.Resources.Edit

        ParentForm.AcceptButton = btnFilter
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ShowEditDialog()
    End Sub

    Private Sub ShowEditDialog()
        ShowEditDialog(New SecurityView)
    End Sub

    Private Sub ShowEditDialog(ByVal securityView As SecurityView)
        Dim securityMaintenanceEditor As New SecurityMaintenanceEditor(securityView)
        If securityMaintenanceEditor.ShowDialog(Me) = DialogResult.OK Then
            LoadGrid()
        End If
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Try
            Dim result = OpenFileDialog.ShowDialog(Me)
            If result = DialogResult.OK Then
                Me.ParentForm.Cursor = Cursors.WaitCursor
                Dim securitysList As List(Of SecurityView) = FileHelper.GetSecuritiesFromCsv(OpenFileDialog.FileName)
                DataScript.LoadSecurities(securitysList)
                MessageBox.Show(String.Format("{0} securities have been added succesfully.", securitysList.Count), "Loaded Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
                LoadGrid()
                Me.ParentForm.Cursor = Cursors.Default
            End If

        Catch ex As Exception
            Me.ParentForm.Cursor = Cursors.Default
            MessageBox.Show("Unable to load the securities into the system." + Environment.NewLine + ex.Message, "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
